package com.lzq.course.business.controller.admin;

import com.lzq.course.server.dto.CategoryDto;
import com.lzq.course.server.dto.PageDto;
import com.lzq.course.server.dto.ResponseDto;
import com.lzq.course.server.service.CategoryService;
import com.lzq.course.server.util.ValidatorUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/admin/category")
public class CategoryController {

    public static final String BUSINESS_NAME = "分类";

    @Resource
    private CategoryService categoryService;

    /**
     * 查询列表 分页查询
     * @param pageDto
     * @return
     */
    @PostMapping("/list")
    public ResponseDto list(@RequestBody PageDto<CategoryDto> pageDto) {
        ResponseDto responseDto = new ResponseDto();
        categoryService.list(pageDto);

        responseDto.setContent(pageDto);
        return responseDto;
    }

    /**
     * 查询所有 分类
     * @return
     */
    @PostMapping("/all")
    public ResponseDto all() {
        ResponseDto responseDto = categoryService.all();

        return responseDto;
    }

    /**
     * 保存
     * @param categoryDto
     * @return
     */
    @PostMapping("/save")
    public ResponseDto save(@RequestBody CategoryDto categoryDto) {

        //保存校验
        ValidatorUtil.require(categoryDto.getParent(), "父id");
        ValidatorUtil.require(categoryDto.getName(), "名称");
        ValidatorUtil.length(categoryDto.getName(),"名称", 1, 50);

        ResponseDto responseDto = new ResponseDto();
        categoryService.save(categoryDto);

        responseDto.setContent(categoryDto);
        return responseDto;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public ResponseDto delete(@PathVariable String id) {
        ResponseDto responseDto = new ResponseDto();
        categoryService.delete(id);

        return responseDto;
    }
}
